package cn.itsource.smartcampus.controller;

import cn.itsource.smartcampus.domain.User;
import cn.itsource.smartcampus.exception.GlobalException;
import cn.itsource.smartcampus.service.UserService;
import cn.itsource.smartcampus.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public AjaxResult login(@RequestBody User u){
        try {
            String token = userService.login(u);
            return AjaxResult.ok("登录成功！", token);
        }catch (GlobalException ge){
            return AjaxResult.fail(ge.getMessage());
        }
        catch (Exception e){
            return AjaxResult.fail("系统异常，请重试！");
        }

    }
}
